* ======================================
* -------- program: Placebosim ---------
* ======================================

	cap program drop placebosim

	program define placebosim


* Select placebo sample
* =====================
	
	
	* Save n_t (number of politicians) in the actual distibution
	use $dta\PanelPoliticians.dta, clear
	gen num = 1
	qui sum num if year == 2015
	local n_pol15 = r(N)
	qui sum num if year == 2019
	local n_pol19 = r(N)

	* Draw n_t "politicians" from the vote-eligible population
	use $dta\Population.dta, clear
	keep idPers knr bsu year
	rename idPers Polit_id
	rename knr Polit_knr
	rename bsu Polit_bsu
	sample `n_pol15' if year == 2015, count
	sample `n_pol19' if year == 2019, count
	gen Polit_Rank = runiform() // placebo rank
	gen Polit_RankPct = Polit_Rank
	gen Polit_Party = "Placebo"
	
	* Save tmp
	sort Polit_id year
	tempfile pl_pols
	save `pl_pols'


* Match voters to "politicians"
* =============================


	use $dta\PanelVoters.dta, clear
	
	run $prog/prog_match $dta/PanelVoters "`pl_pols'" Fam1 idmatch rank
	run $prog/prog_match $dta/PanelVoters "`pl_pols'" Fam2 idmatch rank
	run $prog/prog_match $dta/PanelVoters "`pl_pols'" Work1 netmatch rank
	run $prog/prog_match $dta/PanelVoters "`pl_pols'" Work2 netmatch rank
	run $prog/prog_match $dta/PanelVoters "`pl_pols'" Imm1 netmatch rank
	run $prog/prog_match $dta/PanelVoters "`pl_pols'" Imm2 netmatch rank

	
* Define samples
* ==============
	
	* Family
	gen Fam1 = 1
	gen Fam2 = 1

	* Others
	foreach net in Work1 Work2 Imm1 Imm2{
		rename Voter_id idPers
		merge m:1 idPers using $dta/Network`net'.dta, nogen ///
			keepusing(`net') keep(master matched)
		rename idPers Voter_id
	}
	


* Estimate models
* ===============

	egen idbsu = group(Voter_id Voter_bsu)
	gen period = 1*(year == 2019)
	xtset idbsu period

	foreach net in Fam1 Fam2 Work1 Work2 Imm1 Imm2{
		preserve
		keep if `net' == 1
		xtreg Voter_turnout `net'Any `net'Same i.period, ///
			i(idbsu) fe cluster(Voter_bsu)
		scalar b_`net' = _b[`net'Same]
		restore
		}

	end
